{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "您好！我是一个由 OpenAI 开发的人工智能助手，名字叫做 ChatGPT。我的能力基于大量的文本数据和复杂的算法，这让我可以理解并用多种语言回答不同类型的问题。我的设计目标是在尊重用户隐私的前提下，提供有帮助、有信息量、准确并有趣的回答。\n",
      "\n",
      "在与用户交流时，我尽量提供有用的信息和建议，同时我还可以进行创作，比如撰写故事、诗歌，或者协助解决技术问题和学术问题等。虽然我不是一个真实的人类，但是我试图理解并模拟人类的沟通方式，以便更好地和你进行交流。请随时向我提问，我会尽我所能帮助你！\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"sk-pQ7\"\n",
    "os.environ[\"OPENAI_PROXY\"] = \"https://ai-yyds.com/v1\"\n",
    "\n",
    "#使用openai的官方sdk\n",
    "import openai\n",
    "import os\n",
    "\n",
    "openai.api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "messages = [\n",
    "{\"role\": \"user\", \"content\": \"介绍下你自己\"}\n",
    "]\n",
    "\n",
    "res = openai.ChatCompletion.create(\n",
    "model=\"gpt-4-1106-preview\",\n",
    "messages=messages,\n",
    "stream=False,\n",
    ")\n",
    "\n",
    "print(res['choices'][0]['message']['content'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### OpenAI与ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#调用chatmodels，以openai为例\n",
    "\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.schema.messages import HumanMessage,AIMessage\n",
    "import os\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "chat = ChatOpenAI(\n",
    "    model=\"gpt-4\",\n",
    "    temperature=0,\n",
    "    openai_api_key = api_key,\n",
    "    openai_api_base = api_base\n",
    "\n",
    ")\n",
    "\n",
    "messages = [\n",
    "    AIMessage(role=\"system\",content=\"你好，我是tomie！\"),\n",
    "    HumanMessage(role=\"user\",content=\"你好tomie，我是狗剩!\"),\n",
    "    AIMessage(role=\"system\",content=\"认识你很高兴!\"),\n",
    "    HumanMessage(role=\"user\",content=\"你知道我叫什么吗？\")\n",
    "]\n",
    "\n",
    "response = chat.invoke(messages)\n",
    "print(response)\n",
    "\n",
    "#print(chat.predict(\"你好\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### langchain内置的LLM支持情况"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![LLMs](LLMs.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 流式调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "秋天来了，金色的阳光\n",
      "洒满大地，树叶变黄\n",
      "风儿吹过，吹落叶子\n",
      "像是一场黄金的雨\n",
      "\n",
      "枫叶红了，梧桐黄了\n",
      "枝头摇曳，舞出旋律\n",
      "天空湛蓝，云朵飘过\n",
      "秋风轻拂，凉意袭来\n",
      "\n",
      "田野里，稻谷成熟\n",
      "金黄的麦穗，摇曳生姿\n",
      "农民忙碌，收获的喜悦\n",
      "填满了整个村庄\n",
      "\n",
      "夜晚来临，月光如水\n",
      "星星点点，璀璨夺目\n",
      "秋虫鸣叫，夜幕降临\n",
      "仿佛是大自然的交响乐\n",
      "\n",
      "秋天的美景，让人陶醉\n",
      "让人感受，大自然的魅力\n",
      "秋风吹过，带来清新\n",
      "让我们心中，充满感激\n",
      "\n",
      "秋天来了，万物凋零\n",
      "但却是一种美丽的凋零\n",
      "让我们珍惜，每一个时刻\n",
      "感受秋天，带来的温暖和美好。"
     ]
    }
   ],
   "source": [
    "#LLM类大模型的流式输出方法\n",
    "\n",
    "from langchain.llms import OpenAI\n",
    "import os\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "#构造一个llm\n",
    "llm = OpenAI(\n",
    "    model = \"gpt-3.5-turbo-instruct\",\n",
    "    temperature=0,\n",
    "    openai_api_key = api_key,\n",
    "    openai_api_base = api_base,\n",
    "    max_tokens=512,\n",
    ")\n",
    "\n",
    "for chunk in llm.stream(\"写一首关于秋天的诗歌\"):\n",
    "    print(chunk,end=\"\",flush=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install anthropic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content='' additional_kwargs={} example=False\n",
      "content='' additional_kwargs={} example=False\n",
      "content='秋' additional_kwargs={} example=False\n",
      "content='韵' additional_kwargs={} example=False\n",
      "content='\\n\\n' additional_kwargs={} example=False\n",
      "content='秋' additional_kwargs={} example=False\n",
      "content='风' additional_kwargs={} example=False\n",
      "content='萧' additional_kwargs={} example=False\n",
      "content='瑟' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='落' additional_kwargs={} example=False\n",
      "content='叶' additional_kwargs={} example=False\n",
      "content='纷' additional_kwargs={} example=False\n",
      "content='飞' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='\\n金' additional_kwargs={} example=False\n",
      "content='黄' additional_kwargs={} example=False\n",
      "content='色' additional_kwargs={} example=False\n",
      "content='的' additional_kwargs={} example=False\n",
      "content='世' additional_kwargs={} example=False\n",
      "content='界' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='美' additional_kwargs={} example=False\n",
      "content='不' additional_kwargs={} example=False\n",
      "content='胜' additional_kwargs={} example=False\n",
      "content='收' additional_kwargs={} example=False\n",
      "content='。' additional_kwargs={} example=False\n",
      "content='\\n农' additional_kwargs={} example=False\n",
      "content='夫' additional_kwargs={} example=False\n",
      "content='歌' additional_kwargs={} example=False\n",
      "content='唱' additional_kwargs={} example=False\n",
      "content='着' additional_kwargs={} example=False\n",
      "content='丰' additional_kwargs={} example=False\n",
      "content='收' additional_kwargs={} example=False\n",
      "content='的' additional_kwargs={} example=False\n",
      "content='喜' additional_kwargs={} example=False\n",
      "content='悦' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='\\n' additional_kwargs={} example=False\n",
      "content='孩' additional_kwargs={} example=False\n",
      "content='童' additional_kwargs={} example=False\n",
      "content='嬉' additional_kwargs={} example=False\n",
      "content='戏' additional_kwargs={} example=False\n",
      "content='在' additional_kwargs={} example=False\n",
      "content='稻' additional_kwargs={} example=False\n",
      "content='草' additional_kwargs={} example=False\n",
      "content='堆' additional_kwargs={} example=False\n",
      "content='中' additional_kwargs={} example=False\n",
      "content='。' additional_kwargs={} example=False\n",
      "content='\\n\\n天' additional_kwargs={} example=False\n",
      "content='高' additional_kwargs={} example=False\n",
      "content='云' additional_kwargs={} example=False\n",
      "content='淡' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='雁' additional_kwargs={} example=False\n",
      "content='阵' additional_kwargs={} example=False\n",
      "content='归' additional_kwargs={} example=False\n",
      "content='南' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='\\n' additional_kwargs={} example=False\n",
      "content='凉' additional_kwargs={} example=False\n",
      "content='爽' additional_kwargs={} example=False\n",
      "content='的' additional_kwargs={} example=False\n",
      "content='空' additional_kwargs={} example=False\n",
      "content='气' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='沁' additional_kwargs={} example=False\n",
      "content='人' additional_kwargs={} example=False\n",
      "content='心' additional_kwargs={} example=False\n",
      "content='脾' additional_kwargs={} example=False\n",
      "content='。' additional_kwargs={} example=False\n",
      "content='\\n' additional_kwargs={} example=False\n",
      "content='秋' additional_kwargs={} example=False\n",
      "content='虫' additional_kwargs={} example=False\n",
      "content='低' additional_kwargs={} example=False\n",
      "content='吟' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='诉' additional_kwargs={} example=False\n",
      "content='说' additional_kwargs={} example=False\n",
      "content='离' additional_kwargs={} example=False\n",
      "content='别' additional_kwargs={} example=False\n",
      "content='的' additional_kwargs={} example=False\n",
      "content='愁' additional_kwargs={} example=False\n",
      "content='绪' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='\\n' additional_kwargs={} example=False\n",
      "content='夕' additional_kwargs={} example=False\n",
      "content='阳' additional_kwargs={} example=False\n",
      "content='西' additional_kwargs={} example=False\n",
      "content='下' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='染' additional_kwargs={} example=False\n",
      "content='红' additional_kwargs={} example=False\n",
      "content='了' additional_kwargs={} example=False\n",
      "content='半' additional_kwargs={} example=False\n",
      "content='边' additional_kwargs={} example=False\n",
      "content='天' additional_kwargs={} example=False\n",
      "content='。' additional_kwargs={} example=False\n",
      "content='\\n\\n' additional_kwargs={} example=False\n",
      "content='菊' additional_kwargs={} example=False\n",
      "content='花' additional_kwargs={} example=False\n",
      "content='盛' additional_kwargs={} example=False\n",
      "content='开' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='飘' additional_kwargs={} example=False\n",
      "content='香' additional_kwargs={} example=False\n",
      "content='四' additional_kwargs={} example=False\n",
      "content='溢' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='\\n' additional_kwargs={} example=False\n",
      "content='丹' additional_kwargs={} example=False\n",
      "content='桂' additional_kwargs={} example=False\n",
      "content='飘' additional_kwargs={} example=False\n",
      "content='落' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='铺' additional_kwargs={} example=False\n",
      "content='满' additional_kwargs={} example=False\n",
      "content='山' additional_kwargs={} example=False\n",
      "content='野' additional_kwargs={} example=False\n",
      "content='。' additional_kwargs={} example=False\n",
      "content='\\n' additional_kwargs={} example=False\n",
      "content='秋' additional_kwargs={} example=False\n",
      "content='实' additional_kwargs={} example=False\n",
      "content='累' additional_kwargs={} example=False\n",
      "content='累' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='果' additional_kwargs={} example=False\n",
      "content='农' additional_kwargs={} example=False\n",
      "content='笑' additional_kwargs={} example=False\n",
      "content='逐' additional_kwargs={} example=False\n",
      "content='颜' additional_kwargs={} example=False\n",
      "content='开' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='\\n' additional_kwargs={} example=False\n",
      "content='秋' additional_kwargs={} example=False\n",
      "content='色' additional_kwargs={} example=False\n",
      "content='宜' additional_kwargs={} example=False\n",
      "content='人' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='邀' additional_kwargs={} example=False\n",
      "content='约' additional_kwargs={} example=False\n",
      "content='朋' additional_kwargs={} example=False\n",
      "content='友' additional_kwargs={} example=False\n",
      "content='把' additional_kwargs={} example=False\n",
      "content='酒' additional_kwargs={} example=False\n",
      "content='言' additional_kwargs={} example=False\n",
      "content='欢' additional_kwargs={} example=False\n",
      "content='。' additional_kwargs={} example=False\n",
      "content='\\n\\n' additional_kwargs={} example=False\n",
      "content='秋' additional_kwargs={} example=False\n",
      "content='思' additional_kwargs={} example=False\n",
      "content='万' additional_kwargs={} example=False\n",
      "content='千' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='诗' additional_kwargs={} example=False\n",
      "content='情' additional_kwargs={} example=False\n",
      "content='画' additional_kwargs={} example=False\n",
      "content='意' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='\\n' additional_kwargs={} example=False\n",
      "content='秋' additional_kwargs={} example=False\n",
      "content='日' additional_kwargs={} example=False\n",
      "content='静' additional_kwargs={} example=False\n",
      "content='谧' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='怡' additional_kwargs={} example=False\n",
      "content='然' additional_kwargs={} example=False\n",
      "content='自' additional_kwargs={} example=False\n",
      "content='得' additional_kwargs={} example=False\n",
      "content='。' additional_kwargs={} example=False\n",
      "content='\\n' additional_kwargs={} example=False\n",
      "content='秋' additional_kwargs={} example=False\n",
      "content='天' additional_kwargs={} example=False\n",
      "content='啊' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='多' additional_kwargs={} example=False\n",
      "content='彩' additional_kwargs={} example=False\n",
      "content='而' additional_kwargs={} example=False\n",
      "content='温' additional_kwargs={} example=False\n",
      "content='馨' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='\\n让' additional_kwargs={} example=False\n",
      "content='人' additional_kwargs={} example=False\n",
      "content='流' additional_kwargs={} example=False\n",
      "content='连' additional_kwargs={} example=False\n",
      "content='忘' additional_kwargs={} example=False\n",
      "content='返' additional_kwargs={} example=False\n",
      "content='，' additional_kwargs={} example=False\n",
      "content='陶' additional_kwargs={} example=False\n",
      "content='醉' additional_kwargs={} example=False\n",
      "content='其' additional_kwargs={} example=False\n",
      "content='中' additional_kwargs={} example=False\n",
      "content='。' additional_kwargs={} example=False\n",
      "content='' additional_kwargs={} example=False\n",
      "content='' additional_kwargs={} example=False\n"
     ]
    }
   ],
   "source": [
    "#chatmodels的流式调用方法\n",
    "#使用clade模型\n",
    "\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "import os\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    model = \"claude-3-opus-20240229\",\n",
    "    temperature=0,\n",
    "    openai_api_key = api_key,\n",
    "    openai_api_base = api_base,\n",
    "    max_tokens=512,\n",
    ")\n",
    "for chunk in llm.stream(\"写一首关于秋天的诗歌\"):\n",
    "    print(chunk,end=\"\\n\",flush=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 追踪Token的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "有一天，小明去参加一个面试，面试官问他：“你有什么特长吗？”小明回答：“我可以用手指头数到100。”面试官觉得很奇怪，就让小明演示一下。小明开始用手指头数，数到99的时候突然停下来，面试官问：“怎么了？为什么不继续数下去？”小明回答：“因为我只有10根手指头啊！”面试官顿时无语。\n",
      "Tokens Used: 156\n",
      "\tPrompt Tokens: 7\n",
      "\tCompletion Tokens: 149\n",
      "Successful Requests: 1\n",
      "Total Cost (USD): $0.0\n"
     ]
    }
   ],
   "source": [
    "#LLM的toekn追踪\n",
    "from langchain.llms import OpenAI\n",
    "from langchain.callbacks import get_openai_callback\n",
    "import os\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "#构造一个llm\n",
    "llm = OpenAI(\n",
    "    model = \"gpt-3.5-turbo-instruct\",\n",
    "    temperature=0,\n",
    "    openai_api_key = api_key,\n",
    "    openai_api_base = api_base,\n",
    "    max_tokens=512,\n",
    ")\n",
    "\n",
    "with get_openai_callback() as cb:\n",
    "    result = llm.invoke(\"给我讲一个笑话\")\n",
    "    print(result)\n",
    "    print(cb)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content='好的，这是一个我最近听到的笑话：\\n\\n为什么电脑永远不会感冒？\\n\\n因为它有Windows（窗户）。' additional_kwargs={} example=False\n",
      "Tokens Used: 58\n",
      "\tPrompt Tokens: 14\n",
      "\tCompletion Tokens: 44\n",
      "Successful Requests: 1\n",
      "Total Cost (USD): $0.0030599999999999994\n"
     ]
    }
   ],
   "source": [
    "#chatmodels的token追踪\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.callbacks import get_openai_callback\n",
    "import os\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "llm = ChatOpenAI(\n",
    "    model = \"gpt-4\",\n",
    "    temperature=0,\n",
    "    openai_api_key = api_key,\n",
    "    openai_api_base = api_base,\n",
    "    max_tokens=512,\n",
    ")\n",
    "\n",
    "with get_openai_callback() as cb:\n",
    "    result = llm.invoke(\"给我讲一个笑话\")\n",
    "    print(result)\n",
    "    print(cb)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 自定义输出\n",
    "\n",
    "- 输出函数参数\n",
    "- 输出json\n",
    "- 输出List\n",
    "- 输出日期\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "out_put: {\"setup\": \"为什么猫咪总是喜欢把东西丢到地上？\", \"punchline\": \"因为它们觉得地球是圆的，所以才会有东西掉下来。\"}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Joke(setup='为什么猫咪总是喜欢把东西丢到地上？', punchline='因为它们觉得地球是圆的，所以才会有东西掉下来。')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#讲笑话机器人：希望每次根据指令，可以输出一个这样的笑话(小明是怎么死的？笨死的)\n",
    "\n",
    "from langchain.llms import  OpenAI\n",
    "from langchain.output_parsers import PydanticOutputParser\n",
    "from langchain.prompts import PromptTemplate\n",
    "from langchain.pydantic_v1 import BaseModel,Field,validator\n",
    "from typing import  List\n",
    "import os\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "#构造LLM\n",
    "model = OpenAI(\n",
    "    model = \"gpt-3.5-turbo-instruct\",\n",
    "    temperature=0,\n",
    "    openai_api_key = api_key,\n",
    "    openai_api_base = api_base,\n",
    ")\n",
    "\n",
    "#定义个数据模型，用来描述最终的实例结构\n",
    "class Joke(BaseModel):\n",
    "    setup:str = Field(description=\"设置笑话的问题\")\n",
    "    punchline:str = Field(description=\"回答笑话的答案\")\n",
    "\n",
    "    #验证问题是否符合要求\n",
    "    @validator(\"setup\")\n",
    "    def question_mark(cls,field):\n",
    "        if field[-1] != \"？\":\n",
    "            raise ValueError(\"不符合预期的问题格式!\")\n",
    "        return field\n",
    "\n",
    "#将Joke数据模型传入\n",
    "parser = PydanticOutputParser(pydantic_object=Joke)\n",
    "\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    template = \"回答用户的输入.\\n{format_instructions}\\n{query}\\n\",\n",
    "    input_variables = [\"query\"],\n",
    "    partial_variables = {\"format_instructions\":parser.get_format_instructions()}\n",
    ")\n",
    "\n",
    "prompt_and_model = prompt | model\n",
    "out_put = prompt_and_model.invoke({\"query\":\"给我讲一个笑话\"})\n",
    "print(\"out_put:\",out_put)\n",
    "parser.invoke(out_put)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "1. 小白 (Xiao Bai)\n",
      "2. 小黑 (Xiao Hei)\n",
      "3. 小贝 (Xiao Bei)\n",
      "4. 小旺 (Xiao Wang)\n",
      "5. 小强 (Xiao Qiang)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['1. 小白 (Xiao Bai)\\n2. 小黑 (Xiao Hei)\\n3. 小贝 (Xiao Bei)\\n4. 小旺 (Xiao Wang)\\n5. 小强 (Xiao Qiang)']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#LLM的输出格式化成python list形式，类似['a','b','c']\n",
    "\n",
    "from langchain.output_parsers import  CommaSeparatedListOutputParser\n",
    "from langchain.prompts import  PromptTemplate\n",
    "from langchain.llms import OpenAI\n",
    "import os\n",
    "api_base = os.getenv(\"OPENAI_PROXY\")\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "#构造LLM\n",
    "model = OpenAI(\n",
    "    model = \"gpt-3.5-turbo-instruct\",\n",
    "    temperature=0,\n",
    "    openai_api_key = api_key,\n",
    "    openai_api_base = api_base,\n",
    ")\n",
    "\n",
    "parser = CommaSeparatedListOutputParser()\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    template = \"列出5个{subject}.\\n{format_instructions}\",\n",
    "    input_variables = [\"subject\"],\n",
    "    partial_variables = {\"format_instructions\":parser.get_format_instructions()}\n",
    ")\n",
    "\n",
    "_input = prompt.format(subject=\"常见的小狗的名字\")\n",
    "output = model(_input)\n",
    "print(output)\n",
    "#格式化\n",
    "parser.parse(output)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchains",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
    